﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Manufacturers;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Products.Specs;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Templates.Lucene
{
    public partial class LuceneCurrentlyFiltering : BaseLuceneFilterableControl
    {
        //string url = CommonHelper.GetThisPageUrl(true);
        //string[] alreadyFilteredSpecsQueryStringParams = alreadyFilteredOptions.Select(x => "attr" + x.SpecificationAttributeId.ToString()).ToArray();
        //foreach (string qsp in alreadyFilteredSpecsQueryStringParams)
        //{
        //    //little hack here because we get encoded query strong params from CommonHelper.GetThisPageUrl();
        //    string qsp2 = HttpUtility.UrlPathEncode(qsp);
        //    string qsp3 = HttpUtility.UrlEncodeUnicode(qsp);
        //    url = CommonHelper.RemoveQueryString(url, qsp2);
        //    url = CommonHelper.RemoveQueryString(url, qsp3);
        //}
        //url = excludeQueryStringParams(url);
        //hlRemoveFilter.NavigateUrl = url;

        public override void BindData(LuceneHelper.FilterEventArgs e)
        {
            bool isActive;
            isActive = BindOptions(e.FilterContext.Filters);
            isActive = AdjustState(isActive, BindPriceRange(e.FilterContext.PriceRange));
            isActive = AdjustState(isActive, BindManus(e.FilterContext.Manufacturers));
            isActive = AdjustState(isActive, BindKeywords(e.FilterContext.KeyWords));
            this.Visible = isActive;
        }

        private bool AdjustState(bool a, bool b)
        {
            if (a || b)
            {
                return true;
            }
            return false;
        }

        #region Options

        private bool BindOptions(IList<SpecificationAttributeOption> options)
        {
            if (options.Count == 0) { return false; }

            lvOptions.DataSource = options;
            lvOptions.DataBind();

            return true;
        }

        #endregion

        #region Price Range

        private bool BindPriceRange(PriceRange priceRange)
        {
            if (priceRange == null || !(priceRange.From.HasValue || priceRange.To.HasValue))
            {
                ctrlPrices.Visible = false;
                return false;
            }

            lPriceRange.Text = getPriceRangeString(priceRange.From, priceRange.To);
            hlRemovePriceFilter.NavigateUrl = CommonHelper.RemoveQueryString(CommonHelper.GetThisPageUrl(true), "from");
            hlRemovePriceFilter.NavigateUrl = CommonHelper.RemoveQueryString(hlRemovePriceFilter.NavigateUrl, "to");

            return true;
        }

        private const string PRICE_CSSCLASSNAME = "PriceRange";

        protected string getPriceRangeString(decimal? From, decimal? To)
        {
            string result = string.Empty;
            if (!From.HasValue)
            {
                string toString = PriceHelper.FormatPrice(To.Value, true, false);
                result = string.Format(GetLocaleResourceString("Common.PriceRangeFilter.Under"), string.Format("<span class=\"{0}\">{1}</span>", PRICE_CSSCLASSNAME, toString));
            }
            else if (!To.HasValue)
            {
                string fromString = PriceHelper.FormatPrice(From.Value, true, false);
                result = string.Format(GetLocaleResourceString("Common.PriceRangeFilter.Over"), string.Format("<span class=\"{0}\">{1}</span>", PRICE_CSSCLASSNAME, fromString));
            }
            else
            {
                string fromString = PriceHelper.FormatPrice(From.Value, true, false);
                string toString = PriceHelper.FormatPrice(To.Value, true, false);
                result = string.Format("<span class=\"{0}\">{1}</span> - <span class=\"{0}\">{2}</span>", PRICE_CSSCLASSNAME, fromString, toString);
            }
            return result;
        }

        #endregion

        #region Manufacturer

        public bool BindManus(IList<int> manufacturers)
        {
            if (manufacturers.Count <= 0)
            {
                ctrlManu.Visible = false;
                return false;
            }
            var manufacturer = ManufacturerManager.GetManufacturerById(manufacturers[0]);
            lManufacturer.Text = manufacturer.Name;
            hlRemoveManuFilter.NavigateUrl = CommonHelper.RemoveQueryString(CommonHelper.GetThisPageUrl(true),
                                                                            "ManufacturerID");

            return true;
        }

        #endregion

        #region Keywords

        public bool BindKeywords(string keywords)
        {
            keywords = keywords.Trim();
            if (string.IsNullOrEmpty(keywords))
            {
                ctrlKeywords.Visible = false;
                return false;
            }

            lKeywords.Text = keywords;

            return true;
        }

        #endregion
    }
}